百般波折
幾個注意事項
這專案還有好多地方不懂
這裡不知道為何會出錯
好坎今天
代碼如下
//
// TableViewController.swift
// SmashTag
//
// Created by 大容 林 on 2017/1/4.
// Copyright © 2017年 JojoSwiftWorkShop. All rights reserved.
//
import UIKit
import Twitter
class TableViewController: UITableViewController {
var tweets = [Array<Twitter.Tweet>](){
didSet{
tableView.reloadData()
}
}
var serchText : String? {
didSet{
tweets.removeAll()
searchForTweets()
title = serchText
}
}
private var lastTwitterRequest : TwitterRequest?
private var twitterRequest : TwitterRequest? {
if let query = serchText , !query.isEmpty{
return TwitterRequest(search: query + "-filter:retweets",count: 100)
}
return nil
}
private func searchForTweets(){
if let request = twitterRequest {
lastTwitterRequest = request
request.fetchTweets { [weak weakSelf = self] newTweets in
DispatchQueue.main.async {
// if request == weakSelf?.lastTwitterRequest {
if !newTweets.isEmpty {
weakSelf?.tweets.insert(newTweets, at: 0)
}
// }
weakSelf?.refreshControl?.endRefreshing()
}
}
} else {
self.refreshControl?.endRefreshing()
}
}
override func viewDidLoad() {
super.viewDidLoad()
serchText = "Swift"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return tweets.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return tweets[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tweet", for: indexPath)
let tweet = tweets[indexPath.section][indexPath.row]
cell.textLabel?.text = tweet.text
cell.detailTextLabel?.text = tweet.user.name
// Configure the cell...
return cell
}
}